package com.atmilan.quartz.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.atmilan.quartz.mapper.ProductSkuMapper;
import com.atmilan.quartz.mapper.ProductSkuSupplierMapper;
import com.atmilan.quartz.model.dto.buyer.ProductSkuDTO;
import com.atmilan.quartz.model.dto.buyer.ProductSkuSupplierDTO;
import com.atmilan.quartz.model.entity.buyer.ProductSku;
import com.atmilan.quartz.model.entity.buyer.ProductSkuExample;
import com.atmilan.quartz.model.entity.buyer.ProductSkuSupplier;
import com.atmilan.quartz.model.entity.buyer.ProductSkuSupplierExample;
import com.atmilan.quartz.service.IProductSkuService;
import com.atmilan.quartz.utils.BeanConvert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 商品SKUService业务层处理
 * 
 * @author muyu
 * @date 2020-09-07
 */
@Service
public class ProductSkuServiceImpl extends ServiceImpl<ProductSkuMapper, ProductSku> implements IProductSkuService {
    @Autowired
    private ProductSkuSupplierMapper productSkuSupplierMapper;
    @Autowired
    private ProductSkuMapper productSkuMapper;

    @Override
    public List<ProductSkuDTO> getProductSkuByProductId(Long productId) {
        ProductSkuExample productSkuExample = new ProductSkuExample();
        productSkuExample.createCriteria().andProductIdEqualTo(productId);
        List<ProductSku> list = productSkuMapper.selectByExample(productSkuExample);
        List<ProductSkuDTO> listSkuDTO = BeanConvert.copyBeanList(list, ProductSkuDTO.class);

        List skuIds = new ArrayList();

        for (ProductSkuDTO productSkuDTO : listSkuDTO) {
            skuIds.add(productSkuDTO.getId());
        }

        ProductSkuSupplierExample productSkuSupplierExample = new ProductSkuSupplierExample();
        productSkuSupplierExample.createCriteria().andSkuIdIn(skuIds);
        List<ProductSkuSupplier> productSkuSupplierList = productSkuSupplierMapper.selectByExample(productSkuSupplierExample);

        for (ProductSkuDTO productSkuDTO : listSkuDTO) {

            List<ProductSkuSupplier> list1 = new ArrayList<>();
            for (ProductSkuSupplier productSkuSupplier : productSkuSupplierList) {
                if (productSkuSupplier.getSkuId().equals(productSkuDTO.getId())) {
                    list1.add(productSkuSupplier);
                }
            }
            productSkuDTO.setSupplierList(BeanConvert.copyBeanList(list1, ProductSkuSupplierDTO.class));
        }
        return listSkuDTO;
    }

    /**
     * 修改商品SKU
     * 
     * @param productSku 商品SKU
     * @return 结果
     */
    @Override
    public boolean updateProductSku(ProductSkuDTO productSku) {
        return productSkuMapper.updateByPrimaryKey(BeanConvert.copyBean(productSku, ProductSku.class)) > 0;
    }

    /**
     * 查询商品SKU
     * 
     * @param id 商品SKUID
     * @return 商品SKU
     */
    @Override
    public ProductSkuDTO getProductSkuById(Long id) {
        return BeanConvert.copyBean(productSkuMapper.selectByPrimaryKey(id), ProductSkuDTO.class);
    }

}
